package com.dianjing.dianqun.api;

import java.text.ParseException;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dianjing.dianqun.common.ResponseData;
import com.dianjing.dianqun.common.base.BaseRestController;
import com.dianjing.dianqun.model.Content;
import com.dianjing.dianqun.model.DianPaiContent;
import com.dianjing.dianqun.service.ContentService;
import com.dianjing.dianqun.service.UserService;
import com.dianjing.dianqun.service.dianpai.DianPaiContentService;
import com.dianjing.dianqun.utils.ShareEnum;
import com.dianjing.dianqun.utils.StringUtils;
import com.google.common.collect.Maps;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @date : 2017年10月28日
 * 
 * @author: baoyongtao
 * 
 * @see :
 * 
 */
@Api(description = "系统相关api", value = "SystemApiController")
@RestController
@RequestMapping("/api/system")
public class SystemApiController extends BaseRestController {
	@Autowired
	private UserService userService; 
	
	@Autowired
	private ContentService contentService; 

	@ApiOperation(value = "获取系统通知", notes = "根据用id和上次登录时间获取用户通知")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", required = true, paramType = "path"),
			@ApiImplicitParam(name = "lastTime", value = "上次访问时间（yyyy-MM-dd hh:mm:ss）", required = true, paramType = "query") })
	@GetMapping("/notice/{userId}")
	public ResponseData getNotice(@PathVariable(required = true) Integer userId,
			@RequestParam("lastTime") String lastTime) {
		userId = convertUserId(userId);
		if(userService.selectByPrimaryKey(userId)==null) {
			return new ResponseData(ResponseData.STATUS_ERROR, "用户不存在", userId);
		}
		try {
			DateUtils.parseDate(lastTime, "yyyy-MM-dd hh:mm:ss");
		} catch (ParseException e) {
			return new ResponseData(ResponseData.STATUS_ERROR, "日期类型不正确", null);
		}
		//未读关注律师文章
		Long unreadLawyerCount = contentService.selectContentByFollowCount(userId, 1,lastTime);
		//未读关注律所文章
		Long unreadfirmCount = contentService.selectContentByFollowCount(userId, 2,lastTime);
		Map<String,Long> map = Maps.newConcurrentMap();
		map.put("unreadLawyerCount", unreadLawyerCount);
		map.put("unreadFirmCount", unreadfirmCount);
//		map.put("unreadSystemNotice", 0L);
//		map.put("unreadLawyerCount", 2L);
//		map.put("unreadFirmCount", 5L);
		map.put("unreadSystemNotice", 9L);
		return new ResponseData(ResponseData.STATUS_OK, "请求完成", map);
	}
	
	
	
	
	
	@Value("${system.domain}")
	private String domian;

	@Autowired
	private DianPaiContentService dianPaiContentService;

	@ApiOperation(value = "分享接口", notes = "根据分享类型分享对应连接")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "contentId", value = "文章id", dataType = "int", required = true, paramType = "query"),
			@ApiImplicitParam(name = "type", value = "分享类型：ABOUT(1), DIANPAI(2), CONTENT(3), TEAM(4);", required = true, paramType = "query") })
	@GetMapping("/share")
	@SuppressWarnings("all")
	public ResponseData share(@RequestParam(required = true) Integer contentId,@RequestParam("type") Integer type) {
		Map<Object,Object> map = Maps.newHashMap();
		if(type!=null && ShareEnum.DIANPAI.getType().equals(type)) {
			DianPaiContent content = dianPaiContentService.selectByPrimaryKey(contentId);
			if(null!= content ) {
				JSONObject jsonObject = JSONObject.parseObject(content.getTxt());
				Map<String,JSONArray>  hashMap = (Map<String,JSONArray>)jsonObject.getJSONArray("data").get(0);
				map.put("title",content.getContentExt().getTitle());
				map.put("txt",hashMap.get("msg"));
			}
		}else {
			Content content = contentService.selectByPrimaryKey(contentId);
			if(null!=content) {
				map.put("title",content.getContentExt().getTitle());
				String txt = StringUtils.replaceHtml(content.getContentTxt().getTxt());
				txt = txt.length() > 30? txt.substring(0, 30):txt;
				map.put("txt",txt);
			}
		}
		map.put("shareImg",domian+"/static/images/share_img.png");
		String url = domian + ShareEnum.getURL(type);
		map.put("url", url+"?contentId="+contentId);
		return new ResponseData(ResponseData.STATUS_OK, "请求完成", map);
	}

}
